/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import SQL.SQL;

/**
 *
 * @author Administrator
 */
public class construct {
    
    public static void main(String[] args) throws FileNotFoundException, IOException, WeiboException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String access_token = Auth.Auth4Code.getCode();
        System.out.println("Access token is: " + access_token);
        // String access_token = "f191713d6cd68adab64be81fa860c4cd";
        Weibo weibo = new Weibo();
        weibo.setToken(access_token);
        Users um = new Users();
        String username=Config.username;
        String foldername=Config.folderName;
        String path = "F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\"+foldername+"\\";
        String myUid = Config.uid;
        List<String> friendsIDs = getFriendIDs(myUid, um);
        List<String> biFriendIDs = getBiFriendIDS(myUid,um);         
        int mFcount = friendsIDs.size();
        storeFriendIDs(biFriendIDs, path + "myBiFriendsIDs&Names");
        storeFriendIDs(friendsIDs, path + "myFriendsIDs&Names");
        String friendUid;
        List<String> commonIDs = new ArrayList<String>();
        //List<String> coupledIDs = new ArrayList<String>();
        String filename = path + "network";
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + ".txt")));
        List<String> outNet = new ArrayList<String>();
        List<String> inNet = new ArrayList<String>();
        String link = null;
        for (int i = 0; i < mFcount; i++) {
            friendUid = friendsIDs.get(i);
            //好友关注了我
            if (biFriendIDs.contains(friendUid)) {
                link = friendUid + " " + myUid + "\r\n";
                outNet.add(link);
                link = myUid + " " + friendUid + "\r\n";
                inNet.add(link);
            }
            //我关注了此好友
            link = myUid + " " + friendUid;
            outNet.add(link);
            link = friendUid + " " + myUid;
            inNet.add(link);
            System.out.println(i+1 + "/" + mFcount + " " + friendUid);
            commonIDs = getCommonIDs(friendUid, myUid, um);
            if (commonIDs != null && commonIDs.size() > 0) {

                writer.write(friendUid + " ");
                for (String s : commonIDs) {
                    link = friendUid + " " + s;
                    outNet.add(link);
                    link = s + " " + friendUid;
                    inNet.add(link);
                    writer.write(s + " ");
                }
                writer.write("\r\n");
            }
        }
        writer.close();
        //User user =um.showUserById("13500");
        //System.out.println(user.getName());
        BufferedWriter outNetWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + "outNet.txt")));
        Collections.sort(outNet);
        Collections.sort(inNet);
        for (String s : outNet) {
            outNetWriter.write(s + "\r\n");
        }
        outNetWriter.close();
        BufferedWriter inNetWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + "inNet.txt")));
        for (String s : inNet) {
            inNetWriter.write(s + "\r\n");
        }
        inNetWriter.close();

    }

    public static List<String> getBiFriendIDS(String myUid,Users um) throws WeiboException {
        List<String> biFriendIDs = new ArrayList<String>();
        Friendships fm = new Friendships();    
        try {
            User user = um.showUserById(myUid);
            int friendsCount = user.getFriendsCount();
            int page_count = 200;
            int all_pages = friendsCount / page_count;
            for (int i = 0; i < all_pages + 1; i++) {
                String f[] = fm.getFriendsBilateralIds(myUid,0,new Paging(i+1,page_count));
                biFriendIDs.addAll(Arrays.asList(f));
            }
            System.out.println("I have " + biFriendIDs.size() + "BiFriends.");
            Collections.sort(biFriendIDs);
            return biFriendIDs;
        } catch (WeiboException e) {
            //e.printStackTrace();
            return null;
        }
    }


    public static List<String> getCommonIDs(String uid, String myuid, Users um) {
        List<String> commonIDs = new ArrayList<String>();
        Friendships fm = new Friendships();
        try {
            User user = um.showUserById(uid);
            int friendsCount = user.getFriendsCount();
            int page_count = 200;
            int all_pages = friendsCount / page_count;
            for (int i = 0; i
                    < all_pages + 1; i++) {
                try {
                    List<User> f = fm.getFriendsInCommon(uid, myuid, new Paging(i + 1, 200));
                    if (f == null) {
                        break;
                    } else {
                        for (User s : f) {
                            commonIDs.add(s.getId());
                        }
                    }
                } catch (Exception e) {
                }
            }
            System.out.println(uid + " and I have " + commonIDs.size() + "common friends.");
            Collections.sort(commonIDs);
            //for(String s:commonIDs){
            //System.out.println(s);
            //}
            return commonIDs;
        } catch (WeiboException e) {
            return null;
        }
    }

    public static List<String> getFriendIDs(String uid, Users um) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Friendships fm = new Friendships();
        try {
            User user = um.showUserById(uid);
            int friendsCount = user.getFriendsCount();
            int page_count = 200;
            int all_pages = friendsCount / page_count;
            List<String> friendsIDs = new ArrayList<String>();
            List<User> fs=new ArrayList<User>();
            for (int i = 0; i < all_pages + 1; i++) {
                try{
                    List<User> tfs=fm.getFriendsByID(uid, 200, i*200);
                fs.addAll(tfs);
                }catch(Exception e){
                    e.printStackTrace();
                }
                
                String f[] = fm.getFriendsIds(uid, page_count, i * page_count);
                friendsIDs.addAll(Arrays.asList(f));
            }
            for(User u:fs){
                String id=u.getId();
                String name=u.getName();
                int v=(u.isVerified()?1:0);
                int fc=u.getFriendsCount();
                int fanc=u.getFollowersCount();
                int sc=u.getStatusesCount();
                String[] column={"uid","username","friendsCount","fansCount","statusCount","verified"};
                String[] value={id,name,fc+"",fanc+"",sc+"",v+""};
                String table="wb_demo_user";
                SQL.SQL(SQL.insert(column, value, table));
            }
            System.out.println("I have " + friendsIDs.size() + " Friends.");
            Collections.sort(friendsIDs);
            return friendsIDs;
        } catch (WeiboException e) {
            //e.printStackTrace();
            return null;
        }

    }

    public static void storeFriendIDs(List<String> friendsIDs, String filename) throws FileNotFoundException, IOException, WeiboException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + ".txt")));
        System.out.println("Storing FriendsIDs......");
        Users um = new Users();
        int i = 0;
        int count=friendsIDs.size();
        for (String s : friendsIDs) {
            i += 1;
            System.out.println(i + "/" + count);
            writer.write(s + " ");
            try {
                User usr = um.showUserById(s);
                writer.write(usr.getName());
            } catch (Exception e) {
            }
            writer.write("\r\n");
        }
        writer.close();
    }

    
}
